package com.fingard.dsp.bank.directbank.cbhb02;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.directbank.cbhb01.CBHB01Header;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CBHB02Balance extends CBHB02Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();

        try {
            CBHB02Header bankHeader = new CBHB02Header();
            bankHeader.transType = "SCBQ";
            bankHeader.instSeq = getQryTransID();

            while (p_req7506.readRecord()) {
                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
                tmpRetDetail.actArea = p_req7506.readedDetail.actArea;
                tmpRetDetail.actCur = p_req7506.readedDetail.actCur;

                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeElementString("pyrAct", p_req7506.readedDetail.actNum);
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/Cbhbpac/Message/" + bankHeader.transType + "Res/rspCode");
                        String tmpRetInfo = tmpBkReader.getFirstText("/Cbhbpac/Message/" + bankHeader.transType + "Res/rspMsg");
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = tmpRetInfo;
                        tmpRetDetail.availBal = tmpBkReader.getFirstText("/Cbhbpac/Message/" + bankHeader.transType + "Res/availBalAmt");
                        tmpRetDetail.currBal = tmpBkReader.getFirstText("/Cbhbpac/Message/" + bankHeader.transType + "Res/balAmt");
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }

                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(ex.getMessage());
            WriteBankLogLn(ex);
        }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}